//
//  FindFriendViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FindFriendViewController.h"
#import "Masonry.h"
#import "FriendSystemTableCell.h"
#import "ZWYNetTool.h"
#import "ConcernAndFan.h"
#import "RecommendFriendTableCell.h"
#import "UIView+Frame.h"
#import "PersonalHomeViewController.h"
#import "UserInfo.h"

#define kFriendSystemTableCell @"FriendSystemTableCellIdentifier"
#define kRecommendFriendTableCell @"RecommendFriendTableCellIdentifier"

@interface FindFriendViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *systemFriendArray;
@property (nonatomic, strong) NSArray<UserInfo *> *recommendFriendArray;
@end

@implementation FindFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createTableView];
    [self createData];
}

- (void)createData {
    NSDictionary *weixinDic = @{@"image": @"friend_weixin", @"title": @"找微信好友"};
    NSDictionary *qqDic = @{@"image": @"friend_qq", @"title": @"找QQ好友"};
    NSDictionary *phoneDic = @{@"image": @"friend_phone", @"title": @"找通讯录好友"};
    NSDictionary *weiboDic = @{@"image": @"friend_weibo", @"title": @"找微博好友"};
    self.systemFriendArray = @[weixinDic, qqDic, phoneDic, weiboDic];
    
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:@"http://api.miaotu.net/v2/user/recommend?token=abf776af-6a90-11e6-8ccf-00163e002e59" andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        ConcernAndFan *rootModel = [ConcernAndFan modelWithDic:result];
        self.recommendFriendArray = [NSArray array];
        _recommendFriendArray = rootModel.Items;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.dk_width, self.view.dk_height - 64 - 49) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor colorWithWhite:0.827 alpha:1.000];
    [_tableView registerNib:[UINib nibWithNibName:@"FriendSystemTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kFriendSystemTableCell];
    [_tableView registerNib:[UINib nibWithNibName:@"RecommendFriendTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kRecommendFriendTableCell];
    _tableView.bounces = NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _systemFriendArray.count;
    } else {
        return _recommendFriendArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        FriendSystemTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kFriendSystemTableCell forIndexPath:indexPath];
        NSDictionary *dic = _systemFriendArray[indexPath.row];
        cell.systemImage.image = [UIImage imageNamed:dic[@"image"]];
        cell.titleLabel.text = dic[@"title"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        RecommendFriendTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kRecommendFriendTableCell forIndexPath:indexPath];
        cell.items = _recommendFriendArray[indexPath.row];
        [cell.addFriendButton addTarget:self action:@selector(addFriendAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)addFriendAction:(UIButton *)button {
    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
    NSIndexPath *path = [self.tableView indexPathForCell:cell];
    NSLog(@"点击的button: %ld", (long)path.row);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    } else {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
        UIView *redView = [[UIView alloc] init];
        redView.backgroundColor = [UIColor colorWithRed:0.902 green:0.192 blue:0.000 alpha:1.000];
        [backView addSubview:redView];
        [redView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(backView);
            make.left.mas_equalTo(backView).offset(20);
            make.height.mas_equalTo(backView).multipliedBy(0.3f);
            make.width.mas_equalTo(2);
        }];
        UILabel *infoLabel = [[UILabel alloc] init];
        [backView addSubview:infoLabel];
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(redView);
            make.left.mas_equalTo(redView.mas_right).offset(10);
        }];
        infoLabel.text = @"据说关注他们有肉吃, 推荐!";
        
        return backView;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    } else {
        return 80;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 20;
    } else {
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        PersonalHomeViewController *perHomeVC = [[PersonalHomeViewController alloc] init];
//        perHomeVC.userInfo = _recommendFriendArray[indexPath.row];
        perHomeVC.Uid = _recommendFriendArray[indexPath.row].Uid;
        [self.navigationController pushViewController:perHomeVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
